import Api from '../../http/Api';
import { tokenInterceptor } from '../Interceptors';
import { HttpUtil } from '../../utils/HttpUtil';
import { DateUtil } from '../../utils/DateUtil';
class BaseService extends Api {
	constructor() {
		super(...arguments);
		this.initTokenInstance = key => {
			return this.initSingle(
				key,
				{
					baseURL: `/api/${key}`
				},
				tokenInterceptor
			);
		};
		this.selectOneById = id =>
			this.request({
				method: this.method.GET,
				url: `/id/${id}`
			}).then(resp => {
				HttpUtil.setData(resp, this.time(HttpUtil.getData(resp)));
				return resp;
			});
		this.selectOne = record =>
			this.request({
				method: this.method.POST,
				url: '/search',
				data: record ? this.format(record) : null
			}).then(resp => {
				HttpUtil.setData(resp, this.time(HttpUtil.getData(resp)));
				return resp;
			});
		this.selectByIds = list =>
			this.request({
				method: this.method.POST,
				url: '/list/id',
				data: list
			}).then(resp => {
				HttpUtil.setData(
					resp,
					HttpUtil.getData(resp).map(record => this.time(record))
				);
				return resp;
			});
		this.select = condition =>
			this.request({
				method: this.method.POST,
				url: '/list/search',
				data: {
					...condition,
					query: condition.query ? this.format(condition.query) : null,
					columnCondition: {
						...condition.columnCondition,
						timeConditions: condition.columnCondition?.timeConditions
							? condition.columnCondition.timeConditions.map(timeCondition =>
									DateUtil.formatObject(timeCondition, ['begin', 'end'])
							  )
							: null
					}
				}
			}).then(resp => {
				HttpUtil.setList(
					resp,
					HttpUtil.getList(resp).map(record => this.time(record))
				);
				return resp;
			});
		this.selectLike = condition =>
			this.request({
				method: this.method.POST,
				url: '/list/like',
				data: {
					...condition,
					query: condition.query ? this.format(condition.query) : null,
					columnCondition: {
						...condition.columnCondition,
						timeConditions: condition.columnCondition?.timeConditions
							? condition.columnCondition.timeConditions.map(timeCondition =>
									DateUtil.formatObject(timeCondition, ['begin', 'end'])
							  )
							: null
					}
				}
			}).then(resp => {
				HttpUtil.setList(
					resp,
					HttpUtil.getList(resp).map(record => this.time(record))
				);
				return resp;
			});
		this.selectAll = condition =>
			this.request({
				method: this.method.GET,
				url: '/list',
				data: condition
			}).then(resp => {
				HttpUtil.setList(
					resp,
					HttpUtil.getList(resp).map(record => this.time(record))
				);
				return resp;
			});
		this.selectCount = record =>
			this.request({
				method: this.method.POST,
				url: '/count',
				data: record ? this.format(record) : null
			});
		this.insert = record =>
			this.request({
				method: this.method.POST,
				data: record ? this.format(record) : null
			});
		this.insertList = records =>
			this.request({
				method: this.method.POST,
				url: '/list',
				data: records.map(record => (record ? this.format(record) : null))
			});
		this.save = record =>
			this.request({
				method: this.method.PUT,
				data: record ? this.format(record) : null
			});
		this.saveList = records =>
			this.request({
				method: this.method.PUT,
				url: '/list',
				data: records.map(record => (record ? this.format(record) : null))
			});
		this.update = record =>
			this.request({
				method: this.method.PATCH,
				data: record ? this.format(record) : null
			});
		this.updateList = records =>
			this.request({
				method: this.method.PATCH,
				url: '/list',
				data: records.map(record => (record ? this.format(record) : null))
			});
		this.deleteById = id =>
			this.request({
				method: this.method.DELETE,
				url: `/id/${id}`
			});
		this.deleteByIds = list =>
			this.request({
				method: this.method.DELETE,
				url: '/list/id',
				data: list
			});
	}
}
export class TimeDataService extends BaseService {
	constructor() {
		super();
		this.create();
	}
}
